package huoye.shops.common;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.codeideology.android.utils.StringUtils;
import com.codeideology.android.widget.recycler.RecyclerViewAdapter;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import huoye.shops.R;
import huoye.shops.base.APIUrls;
import huoye.shops.huoye.operation.model.Descript;
import huoye.shops.huoye.operation.model.ShopEventPrizeList;
import huoye.shops.widget.RoundImageView;


/**
 * Created by uesr on 2018/8/10.
 */

public class ImageTextAdapter extends RecyclerViewAdapter<ImageTextAdapter.ViewHolder,Descript> {

    public ImageTextAdapter(Context context) {
        super(context, R.layout.include_add_image_replace);

    }

    public void setMaxCount(int maxCount) {
        this.maxCount = maxCount;
    }

    private int maxCount;

    @Override
    protected ViewHolder initViewHolder(View view, int i) {
        return new ViewHolder(view);
    }

    @Override
    protected void bindView(final ViewHolder viewHolder, final Descript list, final int positon) {

        EditText[] inputEts = {viewHolder.et};
        for (EditText et : inputEts) {
            if (et.getTag() != null && et.getTag() instanceof TextWatcher) {
                et.removeTextChangedListener((TextWatcher) et.getTag());
            }
        }
        showImg(viewHolder, list, positon);
        if (positon == getItemCount() - i) return;
        for (int i = 0; i < inputEts.length; i++) {
            final EditText et = inputEts[i];
            final int index = i;
            TextWatcher tvWatcher = new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (index == 0) {
                        list.setImgDes(TextUtils.isEmpty(s) ? "" : s.toString());
                    }
                }
            };
            et.addTextChangedListener(tvWatcher);
            et.setTag(tvWatcher);
        }


        list.setImgUrl(TextUtils.isEmpty(list.getImgUrl()) ? "" : list.getImgUrl());

       viewHolder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData().remove(positon);
                notifyDataSetChanged();
            }

        });


        viewHolder.btnReplace.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onRecyclerViewItemClickListener.onAddImageClick(positon);
            }
        });



    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == -1) {
            return new ViewHolder((View) this.getHeaderViews().get(0));
        } else if (viewType == -2) {
            return new ViewHolder((View) this.getHeaderViews().get(0));
        } else {
            ViewHolder v = this.onCreateViewDataHolder(this.mLayoutInflater, parent, viewType);
            if (v != null && v.itemView != null) {
                v.itemView.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {
                        int position = (Integer) view.getTag();
                        if (onRecyclerViewItemClickListener != null && position < getItemCount() - 1) {
                            onRecyclerViewItemClickListener.onItemClick(getData().get(position), position, view);
                        } else if (onRecyclerViewItemClickListener != null && position == getItemCount() - 1) {
                            onRecyclerViewItemClickListener.onItemClick(null, position, view);
                        }

                    }
                });
            }

            return v;
        }
    }


    public ViewHolder onCreateViewDataHolder(LayoutInflater inflater, ViewGroup parent, int viewType) {
        View view = this.createHolderView(inflater, parent, viewType);
        return this.initViewHolder(view, viewType);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (this.getItemViewType(position) != -1) {
            if (this.getItemViewType(position) == -2) {
                holder.itemView.setVisibility(View.VISIBLE);
            } else {
                int pos = this.getRealPosition(holder);
                if (pos != this.getListCount()) {
                    Descript data = getData().get(pos);
                    if (data != null) {
                        holder.itemView.setTag(pos);
                        this.bindView(holder, data, pos);
                    }
                } else {
                    holder.itemView.setTag(pos);
                    this.bindView(holder, null, pos);
                }
            }
        }
    }

    int i = 1;

    private void showImg(final ViewHolder viewHolder, final Descript list, final int positon) {
        if (getListCount() == maxCount) {
            //6张图
            if (positon == maxCount) {
               viewHolder.ivDelete.setVisibility(View.GONE);
                viewHolder.llImageText.setVisibility(View.INVISIBLE);
            } else {
              viewHolder.ivDelete.setVisibility(View.VISIBLE);
                viewHolder.llImageText.setVisibility(View.VISIBLE);
                viewHolder.ivAdd.setVisibility(View.INVISIBLE);

                if(getContext() != null && StringUtils.isNotEmpty(list.getImgUrl())){
                    if(list.isLocal()){
                        Glide.with(getContext())
                                .load(new File(list.getImgUrl()))
                                .centerCrop()
                                .placeholder(R.mipmap.ic_wait_logo_big)
                                .error(R.mipmap.ic_error)
                                .into(viewHolder.iv);
                    }else {
                        Glide.with(getContext())
                                .load(APIUrls.COMMON_CONFIG.TYPE_COMMON_ALIYUN+list.getImgUrl())
                                .centerCrop()
                                .placeholder(R.mipmap.ic_wait_logo_big)
                                .error(R.mipmap.ic_error)
                                .into(viewHolder.iv);
                    }
                    viewHolder.btnReplace.setText("替换图片");
                }else {
                    viewHolder.iv.setImageResource(R.mipmap.ic_wait_logo_big);

                    viewHolder.btnReplace.setText("上传");
                }


                viewHolder.et.setText(list.getImgDes());

            }
        } else {
            // 有5张图片。但是 你这里吧最后一张图片换成了 add图标
            if (positon == getItemCount() - i) {
                viewHolder.llImageText.setVisibility(View.GONE);
                viewHolder.ivAdd.setVisibility(View.VISIBLE);

            } else {
                viewHolder.llImageText.setVisibility(View.VISIBLE);
                viewHolder.ivAdd.setVisibility(View.INVISIBLE);
                viewHolder.ivDelete.setVisibility(View.VISIBLE);
                if(getContext() != null && StringUtils.isNotEmpty(list.getImgUrl())){
                    if(list.isLocal()){
                        Glide.with(getContext())
                                .load(new File(list.getImgUrl()))
                                .centerCrop()
                                .placeholder(R.mipmap.ic_wait_logo_big)
                                .error(R.mipmap.ic_error)
                                .into(viewHolder.iv);
                    }else {
                        Glide.with(getContext())
                                .load(APIUrls.COMMON_CONFIG.TYPE_COMMON_ALIYUN+list.getImgUrl())
                                .centerCrop()
                                .placeholder(R.mipmap.ic_wait_logo_big)
                                .error(R.mipmap.ic_error)
                                .into(viewHolder.iv);
                    }
                    viewHolder.btnReplace.setText("替换图片");
                }else {
                    viewHolder.iv.setImageResource(R.mipmap.ic_wait_logo_big);
                    viewHolder.btnReplace.setText("上传");
                }

                viewHolder.et.setText(list.getImgDes());


            }
        }
    }


    @Override
    public void add( Descript list) {
        getData().add(list);
    }

    public static class ViewHolder extends RecyclerViewAdapter.ViewHolder {

        @BindView(R.id.item_add_image_replace_rv)
        RoundImageView iv ;

        @BindView(R.id.item_add_image_replace_iv)
        ImageView ivDelete;

        @BindView(R.id.item_add_image_replace_btn)
        Button btnReplace ;

        @BindView(R.id.item_add_image_replace_et)
        EditText et;

        @BindView(R.id.item_add_image_replace_ll_image_text_import)
        LinearLayout llImageText;

        @BindView(R.id.item_add_image_replace_iv_add)
        ImageView ivAdd;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    @Override
    public int getItemCount() {
        if (getListCount() >= maxCount) {
            return maxCount;
        } else {
            return getData().size() + 1;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position < this.getHeaderViews().size()) {
            return -1;
        } else {
            return position >= this.getHeaderViews().size() + this.getListCount() + 1 ? -2 : this.getDataItemViewType(position - this.getHeaderViews().size());
        }

    }


    protected OnItemClickListener onRecyclerViewItemClickListener;

    public interface OnItemClickListener<T> {
        void onItemClick(T var1, int var2, View var3);
        void onAddImageClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }



}

